<template>
<!-- 文章详情页 -->
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
<van-loading type="spinner" color="#1989fa" size="0.46rem"
v-if="articledetail.title===undefined"/>
    <!-- 文章信息区域 -->
    <div v-else>
    <div class="article-container" >
      <!-- 文章标题 -->
      <h1 class="art-title">{{articledetail.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="articledetail.aut_id" :label="getTime(articledetail.pubdate)">
        <template #icon>
          <img :src="articledetail.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini"
             v-if="articledetail.is_followed"
             @click="togglefollowed(false)">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain
            v-else
            @click="togglefollowed(true)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articledetail.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small"
        v-if="articledetail.attitude==1" @click="toggleattitude(true)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small"
        v-else @click="toggleattitude(false)">点赞</van-button>
      </div>
    </div>
    <!-- 文章评论区 -->
    <CommentList :artId="$route.query.aid || ''"></CommentList>
    <!-- <CommentList :artId="articledetail.art_id" v-if="articledetail.art_id"></CommentList> -->
    <!-- 路由切换,通过route.query.aid 获取不到 -->
    <!-- 页面刚打开articledetail是个空对象，空对象.属性的话是undefined   解决方案vue会先解析指令,如果是空,先不显示组件,数据请求回来之后再显示 -->
</div>
  </div>
</template>

<script>
// 注意这个地方是按需导入,不然就说不是一个方法报错
import { getArticleCheckAPI, AutfollowAPI, AutdisfollowAPI, likeartAPI, dislikeartAPI } from '@/api'
import CommentList from './CommentList.vue'
import { getTime } from '@/utils/datetime.js'
import { Mytitle } from '@/utils/Notify.js'
export default {
  name: 'ArticleDetail',
  data () {
    return {
      artid: this.$route.query.aid, // 指定文章id
      articledetail: {}, // 指定文章的详细信息
      commentlist: [] // 评论列表
    //   articledetail.pubdate
    }
  },
  components: {
    CommentList
  },

  async created () {
    try {
      // 获得指定文章对象
      const res = await getArticleCheckAPI({ id: this.$route.query.aid })
      this.articledetail = res.data.data
    } catch (err) {
      console.dir(err)
      Mytitle({ type: 'warning', message: '文章不存在' })
      setTimeout(() => {
        this.$router.back()
      }, 2000)
    }
  },
  methods: {
    getTime,
    //   切换是否关注作者
    async togglefollowed (bool) {
      // 如果是bool===fales 点击的是关注 ==要关注 显示未关注        articledetail.is_followed=false
      // 如果是bool===true  点击的是已关注 ====要取消关注  显示关注   articledetail.is_followed=true
      if (bool === true) {
        this.articledetail.is_followed = true
        await AutfollowAPI({ target: this.articledetail.aut_id })
      } else {
        this.articledetail.is_followed = false
        await AutdisfollowAPI({ id: this.articledetail.aut_id })
      }
    },
    // 切换是否点赞
    async toggleattitude (bool) {
      // 数据是-1 表示不喜欢  --显示点赞  1 表示喜欢---显示已点赞
      // 如果是bool===fales 点击的是未赞 ==要赞 显示赞   this.articledetail.attitude = 1
      // 如果是bool===true  点击的是已赞 ====要取消赞  显示未赞  this.articledetail.attitude=0
      if (bool === true) {
        this.articledetail.attitude = 0
        // 调不赞的接口
        await dislikeartAPI({ id: this.articledetail.art_id })
      } else {
        this.articledetail.attitude = 1
        // 调赞的接口
        await likeartAPI({ target: this.articledetail.art_id })
      }
    }

  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img{
      width: 100%;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.van-loading{
    text-align: center;
    padding-top:46px;
}
</style>
